package com.synergy.stockflow.production.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.synergy.stockflow.production.create.ReportOrTaskVo;
import com.synergy.stockflow.production.entity.ProductionTask;
import com.synergy.stockflow.production.mapper.ProductionTaskMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.synergy.stockflow.production.mapper.ProductionReportMapper;
import com.synergy.stockflow.production.entity.ProductionReport;
import com.synergy.stockflow.production.service.ProductionReportService;
@Service
public class ProductionReportServiceImpl extends ServiceImpl<ProductionReportMapper, ProductionReport> implements ProductionReportService{


    @Autowired
    private ProductionTaskMapper productionTaskMapper;


    @Override
    public List<ProductionReport> selectAll(Page<ProductionReport> objectPage){
        return baseMapper.selectAll(objectPage);
    }


    @Override
    public void updateReport(String reportId, ReportOrTaskVo updateDTO) {
        ProductionReport report = baseMapper.selectById(reportId);
        if (report == null) {
            throw new RuntimeException("报工单不存在");
        }

        // 更新主表信息
        ProductionReport updateEntity = new ProductionReport();
        BeanUtils.copyProperties(updateDTO, updateEntity);
        updateEntity.setReportId(reportId);


        // 重新计算总数量
        if (updateDTO.getProductionReport().getQualifiedQuantity() != null || updateDTO.getProductionReport().getDefectQuantity() != null) {
            BigDecimal qualifiedQty = updateDTO.getProductionReport().getQualifiedQuantity() != null ?
                    updateDTO.getProductionReport().getQualifiedQuantity() : report.getQualifiedQuantity();
            BigDecimal defectQty = updateDTO.getProductionReport().getDefectQuantity() != null ?
                    updateDTO.getProductionReport().getDefectQuantity() : report.getDefectQuantity();
            updateEntity.setTotalQuantity(qualifiedQty.add(defectQty));
        }

        baseMapper.updateById(updateEntity);

        // 如果更新了任务相关信息，同步更新任务表
        if (updateDTO.getCompletedQuantity() != null || updateDTO.getQualityStatus() != null || updateDTO.getTaskStatus() != null) {
            updateProductionTask(report.getTaskId(), updateDTO);
        }
    }
    private void updateProductionTask(String taskId, ReportOrTaskVo updateDTO) {
        ProductionTask task = new ProductionTask();
        task.setTaskId(taskId);



        if( updateDTO.getTaskStatus() != null){
            task.setTaskStatus(updateDTO.getTaskStatus());
        }

        task.setUpdateTime(new Date());
        productionTaskMapper.updateById(task);
    }


    @Override
    public int updateListStatus(String reportId){
        baseMapper.updateListStatus(reportId);
        return 0;
    }

    @Override
    public int countCurrentMonthPlans() {
        QueryWrapper<ProductionReport> wrapper = new QueryWrapper<>();
        wrapper.apply("DATE_FORMAT(create_time, '%Y%m') = DATE_FORMAT(CURDATE(), '%Y%m')");
        return (int) this.count(wrapper);
    }

    @Override
    public List<ProductionReport> selectByReport(Page<ProductionReport> objectPage,
                                                 @Param("taskId")String taskId,
                                                 @Param("materialName")String materialName,

                                                 @Param("reportPeopleId")String reportPeopleId,
                                                 @Param("checkId")String checkId) {
        return baseMapper.selectByReport(objectPage,taskId, materialName, reportPeopleId, checkId);
    }


}
